package com.udaos.modules.allot.controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.By;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import com.udaos.common.base.Result;
import com.udaos.common.filter.PrivateFilter;
import com.udaos.common.page.DataTableColumn;
import com.udaos.common.page.DataTableOrder;
import com.udaos.models.allot.Ud_allot_agreement;
import com.udaos.models.company.Ud_company;
import com.udaos.models.product.Ud_product_type;
import com.udaos.models.school.Ud_school;
import com.udaos.models.sys.Sys_user;
/**
 * 分成
 * @author mutaotao
 *
 */
import com.udaos.modules.allot.services.UdAllotAgreementService;
@IocBean
@At("/platform/allot")
@Filters({ @By(type = PrivateFilter.class)})
public class UdAlotAgreementController{

	@Inject
	private UdAllotAgreementService allotAgreementService;
	

	/**
	 * 首页
	 */
	@At("")
	@Ok("beetl:/platform/base/allot/index.html")
	@RequiresAuthentication
	public void index() {
		
	}
	
	/**
	 * 加载数据
	 * @param name
	 * @param length
	 * @param start
	 * @param draw
	 * @param order
	 * @param columns
	 * @return
	 */
	@At
	@Ok("json:full")
	@RequiresAuthentication
	public Object data(@Param("length") int length, @Param("start") int start, @Param("draw") int draw, @Param("::order") List<DataTableOrder> order, @Param("::columns") List<DataTableColumn> columns) {
		return allotAgreementService.init(length, start, draw);
	}
	
	
	/**
	 * 新增
	 * @return
	 */
	@At
	@Ok("beetl:/platform/base/allot/add.html")
	@RequiresAuthentication
	public Object add() {
		Map<String,Object> map=new HashMap<String,Object>();
		//获取主体
		List<Ud_company> companyList=allotAgreementService.findCompany();
		//获取产品类型
		List<Ud_product_type> productTypeList=allotAgreementService.findProductTypeList();
		map.put("companyList", companyList);
		map.put("productTypeList", productTypeList);
		return map;
	}
	
	/**
	 * 新增协议
	 * @param company
	 * @param req
	 * @return
	 */
	@At
	@Ok("json")
	@RequiresAuthentication
	public Object addDo(@Param("..") Ud_allot_agreement agreement, HttpServletRequest req) {
		try {
			//设置创建时间
			agreement.setCreateAt((int)(System.currentTimeMillis()/1000));
			//设置创建人
			Subject subject = SecurityUtils.getSubject();
            Sys_user user = (Sys_user) subject.getPrincipal();
            agreement.setCreateBy(user.getId());
            allotAgreementService.insert(agreement);
	        return Result.success("保存成功");
	    } catch (Exception e) {
	    	return Result.error(e.getMessage());
	    }
	}
	/**
	 * 编辑
	 * @param id
	 * @return
	 */
	@At("/edit/?")
	@Ok("beetl:/platform/base/allot/edit.html")
	@RequiresAuthentication
	public Object edit(String id) {
		Map<String,Object> map=new HashMap<String,Object>();
		//获取协议实体
		Ud_allot_agreement agreement= allotAgreementService.fetch(id);
		String  companyName=allotAgreementService.getCompanyName(agreement.getCompany_id());
		String  schoolName=allotAgreementService.getSchoolName(agreement.getSchool_id());
		String  productTypeName=allotAgreementService.getProductTypeName(agreement.getProduct_type());
		map.put("companyName", companyName);
		map.put("schoolName", schoolName);
		map.put("productTypeName", productTypeName);
		map.put("agreement", agreement);
		
		return map;
	}
	/**
	 * 修改
	 * @param member
	 * @param req
	 * @return
	 */
	@At
	@Ok("json")
	@RequiresAuthentication
	public Object editDo(@Param("..")  Ud_allot_agreement agreement, HttpServletRequest req) {
		try {
			agreement.setOpAt((int)(System.currentTimeMillis()/1000));
			allotAgreementService.updateIgnoreNull(agreement);
	   		return Result.success("保存成功");
		} catch (Exception e) {
			return Result.error(e.getMessage());
		}
	}
	
	/**
	 * 删除
	 * @param id
	 * @param req
	 * @return
	 */
	@At("/delete/?")
    @Ok("json")
	@RequiresAuthentication
    public Object delete(String id, HttpServletRequest req) {
        try {
        	Ud_allot_agreement agreement= allotAgreementService.fetch(id);
        	agreement.setDelFlag(true);
        	allotAgreementService.updateIgnoreNull(agreement);
            return Result.success("删除成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
	
	/**
	 * 获取主体开通的驾校
	 * @param company_id
	 * @return
	 */
	@At
	@Ok("json") 
	public List<Ud_school> chooseSchool(String company_id){
		return allotAgreementService.chooseSchool(company_id);
	}
}
